<?php
class NewsAction extends BaseAction {
	var $url = "__APP__/news";
	function index(){
		$c_id = $_GET['cid'];
		$news_id = $_GET['nid'];
		
		$this->assign('cato_output',$this->tree);	
		$url = "News:news_list";
		
		$news = d("news_view");
		import("ORG.Util.Page");
		
		$count = $news->count();
		$p  = new Page ($count, 20 ); 
		
		$p->setConfig('header','篇记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');
            
		if(!empty($c_id)){
			$condition = "C_id = ".$_GET['cid'];
		}
		elseif(!empty($news_id)){
			
			$condition = "News_id = '".$news_id."'";
			$url = "News:news_content";
			$this->assign ( "nowp", $_GET['nowp']);
		}
		else{
			$condition = '1';
		}
		//echo ($condition);
		
		$list=$news->where($condition)->order('News_time desc')->limit($p->firstRow.','.$p->listRows)->findAll(); 
		$page = $p->show (); 
		$this->assign ( "c_id", $c_id );
		$this->assign ( "page", $page );
        $this->assign ( "list", $list );
        $this->assign ("current_p",$p->nowPage);
        

		$this->display($url);
	}
	
		function make_newsid($time,$c_id){
		$n1 = date("YmdHis",$time);
		$n2 = intval(rand(1,100));
		
		return $id=$c_id.$n1.$n2;
	
	}
	
	function news_add(){
		
		if($_POST['news_bt']){
			$new_arr['C_id'] = $_POST['cato_tree'];
			$new_arr['News_title'] = $_POST['news_title'];
			$new_arr['News_content'] = stripslashes($_POST['news_content']);
			$new_arr['News_time'] = time();
			$new_arr['News_id'] = $this->make_newsid($new_arr['News_time'],$new_arr['C_id']);
			
			if(d("news_list")->add($new_arr)){
				$this->assign('jumpUrl',"__APP__/news/index/nid/".$new_arr['News_id']); 
				$this->success("添加新闻成功!");
			}
			//echo spClass("cat_news_list_t")->dumpSql();
			//dump($new_arr);
			//$this->news_make($new_arr['News_id']);
			
		}
		else{
			$this->assign('cato_output',$this->tree);	
			//dump($news);
			$this->display("news:news_add");
		}
	}
	
	function news_del(){
		//echo $_POST['news_row'];
		$news_id = implode("','",$_POST['news_row']);
		//echo $news_id;
		
		//echo "News_id in ('".$news_id."')";
		if($news_id){
			if(d('news_list')->where("News_id in ('".$news_id."')")->delete()){
				$msg = "删除新闻成功!";
			}
			else{
				$msg = "删除新闻失败!";
			}
		}
		else{
			$msg = "news_id不能为空";
		}
		$this->assign('jumpUrl',$this->url); 
		$this->success($msg);
	}
	
		
	function news_edit(){
		
		if($_POST['news_bt']){
			$new_arr['News_id'] = $_POST['news_id'];
			$new_arr['News_title'] = $_POST['news_title'];
			$new_arr['News_content'] = stripslashes($_POST['news_content']);
			$new_arr['C_id'] = $_POST['cato_tree'];
			$new_arr['News_time'] = time();
			$condition = "News_id = '".$new_arr['News_id']."'";
			if(d("news_list")->where($condition)->save($new_arr)){
				$msg = "新闻修改成功!";
				$url = "__APP__/news/index/nid/".$new_arr['News_id'];
				
			}
			else{
				$msg = "新闻修改失败!";
				$url = "__APP__/news/news_add";
			}
			//$this->news_make($new_arr['News_id']);	
			$this->assign('jumpUrl',$url); 
		    $this->success($msg);		
		}
		else{
			$news_id = $_GET['news_id'];
			if($news_id){
				$this->assign('news', d("news_list")->where("News_id = '".$news_id."'")->find());
			}
		
			//dump($news);
			$this->assign('cato_output',$this->tree);
			$this->display("news:news_add");
		}
	}
	
	function news_catch(){
		
		$type = $_GET['type'];
	
		if(empty($type)){
			
			if(!file('catch_list.txt')){
				$catch_news = file_get_contents("http://news.163.com/special/00013C0B/bianjitj.html");
				//将新闻列表地址传给$catch_news;
				
				//echo $catch_news;
				
				preg_match_all("/<div\s+class\=\"left-1\s+left\">(.+?)div/is", $catch_news, $catch_list);
				//获取新闻url所属的div块;
				
				//varbug ($catch_list);
				
				preg_match_all("/href=\"http\:\/\/news\.163\.com\/10(.+?)\.html\"/is", $catch_list[1][0], $catch_list_url);
				//获取url地址;
				
				
				//varbug($catch_list_url);
				
				foreach ($catch_list_url[1] as $value){
					$catch_list_result[] = 'http://news.163.com/10'.$value.'.html';
				}
				//组合成html地址;
				//varbug($catch_list_result);
				
				$result_str = implode("\n", $catch_list_result);	//将存有地址的array 用"\n"连接成字符串;
				file_put_contents('catch_list.txt', $result_str);	//存成text文件;
			}
			
			$this->assign('jumpUrl',"__APP__/news/news_catch/type/catch");
			$this->success('列表生成完成，开始采集:');		 
			
			}
		elseif($type=="catch"){
			//varbug ($urls_arr);
			ob_start();
			
			$c_id = 11;		//采集的新闻所属分类id
			$i = $_GET['i']?$_GET['i']:0;	
			$urls_arr = file('catch_list.txt');		//读取列表地址;
			
			if($i == count($urls_arr)){
				file_put_contents('catch_list.txt', "");
				$this->assign('jumpUrl',$this->url);
				$this->success('采集完成!');		 
				exit;
			}
			$url = $urls_arr[$i];		//将第i条地址赋值给$url;
			//echo $url;
			
			$i++;
			$url2 = '__APP__/news/news_catch/type/catch/i/'.$i;
			
			if(!@file_get_contents(trim($url))){
				$this->assign('jumpUrl',$url2);
				$this->error('采集失败!');
				exit;		 
			}
				
			$url_txt = @file_get_contents(trim($url));//获取新闻页面;
			
			preg_match_all("/<title>(.+?)_/is", $url_txt, $title);		//获取标题;
			//echo $url_txt;
			preg_match_all("/<div\s+class\=\"endContent(.+?)end_i.gif/is", $url_txt, $content);		//获取新闻内容;
		    //echo $content[1][0];
		    
		    if (substr($content[1][0],0,2)=="\">"){
				$content[1][0] = substr($content[1][0],2);
			}
			
			$content[1][0] = preg_replace("/<iframe(.+?)iframe>/is", '', $content[1][0]);		//去除iframe中的广告;
			
			$content[1][0] = str_replace("<h1 id=\"h1title\">", '<h3><div align="middle">', $content[1][0]);		//将标题h1改为h3;
			$content[1][0] = str_replace("</h1>", '</div></h3>', $content[1][0]);			//将标题h1改为h3,并居中;
			
			$content[1][0] = str_replace('<a href="http://news.163.com/"><img src="http://img1.cache.netease.com/cnews/img07/', '', $content[1][0]);
			
			$content[1][0] = preg_replace("/<span(.+?)<\/a><\/span>/is", '', $content[1][0]);
			//$content[1][0] = strip_tags($content[1][0], '<h1>,<div>, <p>, <img>');
		
			$title[1][0] = iconv('GB2312', 'UTF-8', $title[1][0]);		//转码， 必须否则无法入库;
			$content[1][0] = iconv('GB2312', 'UTF-8', $content[1][0]);
			
			
			$news_time = time();
			$news_id = $this->make_newsid($news_time,$c_id);
			
			//入库
			$news_result = array("News_id"=>$news_id,"News_title"=>$title[1][0], "News_content"=>$content[1][0],"News_time"=>$news_time,"C_id"=>$c_id,"catch_flag"=>1);
			if(d("news_list")->add($news_result)){
				//$this->news_make($news_id);
	            $this->assign('jumpUrl',$url2);
				$this->success('正在采集： '.$title[1][0]);
			}
			else{
				$this->assign('jumpUrl',$url2);
				$this->success('入库失败!');
			}
		}
		else{
			redirect(PHP_FILE);
		}
	}
	
	/*function news_make($news_arr="",$cato_id =""){
			
		$news = spClass("cat_news_list_t");
		
		if($news_arr=="" && $cato_id =="" ){
			$news_arr = $this->spArgs("news_row");
			$cato_id = $this->spArgs("cid");
		}
		
		
		if(!is_array($news_arr)){
			$news_arr = array($news_arr);
		}
		
		//dump($news_arr);
		
		if(empty($news_arr) && $cato_id ==""){		//如果分类和新闻都为空，则全部生成html；
			$result = $news->findall();
			foreach($result as $k=>$v){ // 循环
                $news_arr[] = $v['News_id']; 
            }
		}
		elseif(!empty($news_arr)){		//如果新闻列表不为空，则传入$news_arr生成html;
			
		}
		elseif(empty($news_arr) && $cato_id !=""){		//如果新闻列表为空，分类不为空，则传入$cato_id生成html;
		
			$result = $news->findall(array("C_id"=>$cato_id));
			foreach($result as $k=>$v){ // 循环
                $news_arr[] = $v['News_id']; 
            }
		}
		
        if( $news_arr!="" ){ // 获取到全部的留言来进行spUrl的构造
            foreach($news_arr as $value){ // 循环
                $urls[] =  array(
                    array('information','news',array('nid'=>$value))  // 这里和spUrl的参数是相同的
                );
            }
        }
        
        //dump($urls);
		spClass('spHtml')->makeall($urls);
		
		if($this->spArgs("news_sub")){
			echo "making success!";
			echo "<script type='text/javascript'> setTimeout(\"self.location = 'index.php?c=information&a=news'\",3000)</script>";exit;
		}	
	}*/
	
}

?>